home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libmpeg_src.lha / globals.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-25  |  11.2 KB  |  401 lines

  1. /*
  2.  * Copyright (c) 1994 by Gregory P. Ward.
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of the MNI front end to the Berkeley MPEG decoder.
  6.  * 
  7.  * Permission to use, copy, modify, and distribute this software and its
  8.  * documentation for any purpose, without fee, and without written agreement is
  9.  * hereby granted, provided that the above copyright notice and the following
  10.  * two paragraphs appear in all copies of this software.
  11.  * 
  12.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
  13.  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  14.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
  15.  * UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  16.  * SUCH DAMAGE.
  17.  *
  18.  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
  19.  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20.  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER
  21.  * IS ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO PROVIDE
  22.  * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.  
  23.  */
  24.  
  25. /* ----------------------------- MNI Header -----------------------------------
  26. @NAME       : globals.c
  27. @INPUT      : 
  28. @OUTPUT     : 
  29. @RETURNS    : 
  30. @DESCRIPTION: Definitions of global variables and functions used 
  31.               internally by the MPEG video decoder.  The functions 
  32.               defined in this file are those that are called by the 
  33.               Berkeley decoding engine, but aren't really integral
  34.               to decoding MPEG's, namely functions to read data from
  35.               the stream and to "do something" with each frame after 
  36.               it is decoded.
  37.  
  38.               Global variables defined here, and declared in globals.h
  39.               (i.e. those used by the decoding engine itself)
  40.                  ditherType
  41.                  input
  42.  
  43.           Functions defined here:
  44.              dprintf ()
  45.              get_more_data ()
  46.          DoDitherImage ()
  47.          ExecuteDisplay ()
  48. @METHOD     : 
  49. @GLOBALS    : 
  50. @CALLS      : 
  51. @CREATED    : Greg Ward, 94/6/16.
  52. @MODIFIED   : 
  53. ---------------------------------------------------------------------------- */
  54.  
  55. #include <config.h>
  56. #include <stdio.h>
  57. #include <sys/types.h>   /* to make netinet/in.h happy */
  58. #include <netinet/in.h>  /* for htonl() [there is no "real" net stuff here!] */
  59. #include "video.h"
  60. #include "globals.h"
  61. #ifdef DEBUG
  62. #include <stdarg.h>        /* for dprintf () */
  63. #endif
  64.  
  65.  
  66. /* Universal global variables -- those needed by the decoding engine: */
  67.  
  68. DitherEnum  ditherType = FULL_COLOR_DITHER;
  69. FILE       *input;        /* file pointer to incoming data. */
  70.  
  71. /* Global variables used only in this source file: */
  72.  
  73. Boolean EOF_flag = FALSE; /* have we reached end of input stream? */
  74.  
  75. /* Declarations for global variables shared between this file and 
  76.  * wrapper.c (all are defined in wrapper.c)
  77.  */
  78.  
  79. extern Boolean   FrameDone;
  80. extern char     *CurrentImage;
  81. extern ImageDesc ImageInfo;
  82. extern int       totNumFrames;
  83.  
  84. #ifdef DEBUG
  85. int dprintf (char *format, ...)
  86. {
  87.    va_list  arglist;
  88.    int      n;
  89.    
  90.    va_start (arglist, format);
  91.    n = vprintf (format, arglist);
  92.    va_end (arglist);
  93.    return (n);
  94. }
  95. #else
  96. int dprintf (char *format, ...) {}
  97. #endif
  98.  
  99.  
  100. /* ----------------------------- MNI Header -----------------------------------
  101. @NAME       : get_more_data
  102. @INPUT      : buf_start
  103.               max_length
  104.               length_ptr
  105.               buf_ptr
  106. @OUTPUT     : 
  107. @RETURNS    : Returns 1 if data read, 0 if EOF, -1 if error.
  108. @DESCRIPTION: Called by correct_underflow in bit parsing utilities to
  109.               read in more data.  Updates input buffer and buffer length.
  110.  
  111.               Note that this function is called in a rather sneaky,
  112.               implicit manner -- the decoding routines call macros
  113.               in util.h, and those macros call get_more_data.
  114. @METHOD     : 
  115. @GLOBALS    : input - file pointer to the open MPEG stream
  116. @CALLS      : 
  117. @CREATED    : (taken from the original Berkeley code)
  118. @MODIFIED   : 
  119. ---------------------------------------------------------------------------- */
  120. int 
  121. get_more_data(buf_start, max_length, length_ptr, buf_ptr)
  122.      unsigned int *buf_start;
  123.      int max_length;
  124.      int *length_ptr;
  125.      unsigned int **buf_ptr;
  126. {
  127.   
  128.   int length, num_read, i, request;
  129.   unsigned char *buffer, *mark;
  130.   unsigned int *lmark;
  131.  
  132.   if (EOF_flag) return 0;
  133.  
  134.   length = *length_ptr;
  135.   buffer = (unsigned char *) *buf_ptr;
  136.  
  137.   if (length > 0) {
  138.     memcpy((unsigned char *) buf_start, buffer, (length*4));
  139.     mark = ((unsigned char *) (buf_start + length));
  140.   }
  141.   else {
  142.     mark = (unsigned char *) buf_start;
  143.     length = 0;
  144.   }
  145.  
  146.   request = (max_length-length)*4;
  147.   
  148.   num_read = fread( mark, 1, request, input);
  149.  
  150.   /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
  151.   {
  152.     int num_read_rounded;
  153.     unsigned char *index;
  154.  
  155.     num_read_rounded = 4*(num_read/4);
  156.  
  157.     /* this can happen only if num_read<request; i.e. end of file reached */
  158.     if( num_read_rounded < num_read )
  159.       { 
  160.      num_read_rounded = 4*( num_read/4+1 );
  161.      /* fill in with zeros */
  162.      for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
  163.      /* advance to the next 4-byte boundary */
  164.      num_read = num_read_rounded;
  165.       }
  166.   }
  167.   
  168.   if   (num_read < 0) {
  169.     return -1;
  170.   }
  171.   else if (num_read == 0) {
  172.     *buf_ptr = buf_start;
  173.     
  174.     /* Make 32 bits after end equal to 0 and 32
  175.        bits after that equal to seq end code
  176.        in order to prevent messy data from infinite
  177.        recursion.
  178.     */
  179.  
  180.     *(buf_start + length) = 0x0;
  181.     *(buf_start + length+1) = SEQ_END_CODE;
  182.  
  183.     EOF_flag = 1;
  184.     return 0;
  185.   }
  186.  
  187.   lmark = (unsigned int *) mark;
  188.  
  189.   num_read = num_read/4;
  190.  
  191.   for (i=0; i<num_read; i++) {
  192.     *lmark = htonl(*lmark);
  193.     lmark++;
  194.   }
  195.  
  196.   *buf_ptr = buf_start;
  197.   *length_ptr = length + num_read;
  198.  
  199.   return 1;
  200. }    /* get_more_data () */
  201.  
  202.  
  203.  
  204. #if (ENABLE_DITHER)
  205. /* ----------------------------- MNI Header -----------------------------------
  206. @NAME       : DoDitherImage
  207. @INPUT      : l, Cr, Cb - pointers to the luminance, Cr, and Cb planes
  208.               disp - ?
  209.               h, w - height and width of image (?)
  210. @OUTPUT     : 
  211. @RETURNS    : 
  212. @DESCRIPTION: Called when image needs to be dithered. Selects correct
  213.               dither routine based on info in ditherType.
  214. @METHOD     : 
  215. @GLOBALS    : ditherType
  216. @CALLS      : One of the following, depending on the value of ditherType:
  217.                  HybridDitherImage       (hybrid.c)
  218.          HybridErrorDitherImage  (hybriderr.c)
  219.          FS2FastDitherImage      (fs2fast.c)
  220.          FS2DitherImage          (fs2.c)
  221.          FS4DitherImage          (fs4.c)
  222.          Twox2DitherImage        (2x2.c)
  223.          ColorDitherImage        (24bit.c)
  224.          GrayDitherImage         (gray.c)
  225.          OrderedDitherImage      (ordered.c)
  226.          MonoDitherImage         (mono.c)
  227.          MonoThresholdImage      (mono.c)
  228.          Ordered2DitherImage     (ordered2.c)
  229.          MBOrderedDitherImage    (mb_ordered.c)
  230. @CREATED    : (taken from the original Berkeley code)
  231. @MODIFIED   : 
  232. ---------------------------------------------------------------------------- */
  233. void
  234. DoDitherImage(l, Cr, Cb, disp, h, w) 
  235. unsigned char *l, *Cr, *Cb, *disp;
  236. int h,w;
  237. {
  238.  
  239.   switch(ditherType) {
  240.   case HYBRID_DITHER:
  241.     HybridDitherImage(l, Cr, Cb, disp, h, w);
  242.     break;
  243.  
  244.   case HYBRID2_DITHER:
  245.     HybridErrorDitherImage(l, Cr, Cb, disp, h, w);
  246.     break;
  247.  
  248.   case FS2FAST_DITHER:
  249.     FS2FastDitherImage(l, Cr, Cb, disp, h, w);
  250.     break;
  251.  
  252.   case FS2_DITHER:
  253.     FS2DitherImage(l, Cr, Cb, disp, h, w);
  254.     break;
  255.  
  256.   case FS4_DITHER:
  257.     FS4DitherImage(l, Cr, Cb, disp, h, w);
  258.     break;
  259.  
  260.   case Twox2_DITHER:
  261.     Twox2DitherImage(l, Cr, Cb, disp, h, w);
  262.     break;
  263.  
  264.   case FULL_COLOR_DITHER:
  265.     ColorDitherImage(l, Cr, Cb, disp, h, w);
  266.     break;
  267.  
  268.   case GRAY_DITHER:
  269.     GrayDitherImage(l, Cr, Cb, disp, h, w);
  270.     break;
  271.  
  272.   case NO_DITHER:
  273.     break;
  274.  
  275.   case ORDERED_DITHER:
  276.     OrderedDitherImage(l, Cr, Cb, disp, h, w);
  277.     break;
  278.  
  279.   case MONO_DITHER:
  280.     MonoDitherImage(l, Cr, Cb, disp, h, w);
  281.     break;
  282.  
  283.   case MONO_THRESHOLD:
  284.     MonoThresholdImage(l, Cr, Cb, disp, h, w);
  285.     break;
  286.  
  287.   case ORDERED2_DITHER:
  288.     Ordered2DitherImage(l, Cr, Cb, disp, h, w);
  289.     break;
  290.  
  291.   case MBORDERED_DITHER:
  292.     MBOrderedDitherImage(l, Cr, Cb, disp, h, w);
  293.  
  294.  
  295.     break;
  296.   }
  297. }   /* DoDitherImage () */
  298.  
  299. #endif
  300.  
  301.  
  302.  
  303. #ifdef DEBUG
  304. long ImageCheckSum (long Size, char *Data)
  305. {
  306.    long    *LongData = (long *) Data;
  307.    long     Sum = 0;
  308.    int     i, LongSize;
  309.  
  310.    LongSize = Size / sizeof(long);
  311.  
  312.    for (i = 0; i < LongSize; i++)
  313.       Sum += LongData[i];
  314.   
  315.    return (Sum);      
  316. }
  317. #endif
  318.  
  319.  
  320. #ifdef DEBUG
  321. void PrintVidStreamStatus (VidStream *stream)
  322. {
  323.    int    i;
  324.  
  325.    printf ("  bit offset in stream    = %8d\n", stream->bit_offset);
  326.    printf ("  remaining buffer length = %8d\n", stream->buf_length);
  327.    printf ("  buffer contents:");
  328.    for (i = 0; i < 5; i++) printf (" %08X", stream->buf_start[i]);
  329.    putchar ('\n');
  330.  
  331.    printf ("  ring buffer of images:");
  332.    for (i = 0; i < RING_BUF_SIZE; i++) printf (" %08X", stream->ring[i]);
  333.    putchar ('\n');
  334.  
  335.    printf ("     past image = %08X\n", stream->past);
  336.    printf ("   future image = %08X\n", stream->future);
  337.    printf ("  current image = %08X\n", stream->current);
  338.    
  339. }
  340. #else
  341. void PrintVidStreamStatus (VidStream *stream)
  342. {
  343. }
  344. #endif
  345.  
  346.  
  347.  
  348. /* ----------------------------- MNI Header -----------------------------------
  349. @NAME       : ExecuteDisplay
  350. @INPUT      : vid_stream - the currently-being-decoded video stream.
  351.               The frame with which we are concerned is points to by
  352.           vid_stream->current->display.
  353. @OUTPUT     : 
  354. @RETURNS    : 
  355. @DESCRIPTION:Called by the MPEG decoder after each frame has been
  356.              decoded.  In the original (Berkeley) implementation,
  357.              ExecuteDisplay called Xlib routines (XCreateImage,
  358.              XPutImage) to display the just-decoded image immediately,
  359.              but now all it does is update the frame counter, set the
  360.              global variable CurrentImage (shared between wrapper.c
  361.              and this file) to point to the image data, and set 
  362.          FrameDone to TRUE so that GetMPEGFrame() will know that
  363.          we have received another finished frame.
  364. @METHOD     : 
  365. @GLOBALS    : 
  366. @CALLS      : 
  367. @CREATED    : Greg Ward, 94/6/16 (almost nothing remains of the 
  368.               ExecuteDisplay() in the original Berkeley source,
  369.           so I'm claiming creator status.)
  370. @MODIFIED   : 
  371. ---------------------------------------------------------------------------- */
  372. void ExecuteDisplay(VidStream *vid_stream)
  373. {
  374.    if (ditherType == NO_DITHER) return;
  375.  
  376. #ifdef DEBUG
  377.    printf ("ExecuteDisplay:\n");
  378.    PrintVidStreamStatus (vid_stream);
  379.    printf ("  frame %2d: image checksum = %08X\n\n", totNumFrames,
  380.         ImageCheckSum (vid_stream->h_size*vid_stream->v_size*4, 
  381.                vid_stream->current->display));
  382. #endif
  383.    
  384.    totNumFrames++;
  385.    
  386.    /* 
  387.     * vid_stream->current->display points to the actual image data;
  388.     * the actual format is described by the ImageDesc structure (which
  389.     * was based on the XImage structure created by ExecuteDisplay() in
  390.     * the original Berkeley code), and can be displayed however 
  391.     * you like.
  392.     *
  393.     * Right now, we just copy the image data to a newly-allocated
  394.     * area, which is pointed to by ImageData[totNumFrames].  Later we
  395.     * will display these saved images as fast as possible.  
  396.     */
  397.    
  398.    CurrentImage = (char *) vid_stream->current->display;
  399.    FrameDone = TRUE;
  400. }  /* ExecuteDisplay () */
  401.